<?php
// +----------------------------------------------------------------------
// | HkCms 网站前台主题管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\template\controller;

use common\controller\AdminBase;

class Theme extends AdminBase {

    //主题显示
    public function index()
    {
        $filed = glob(TEMPLATE_PATH . '*');
        $count = 0;
        $arr = [];
        foreach ($filed as $key => $v) {
            if (is_dir($v) == false) {
                continue;
            }
            $arr[$key]['name'] = basename($v);
            if (is_file(TEMPLATE_PATH . $arr[$key]['name'] . '/preview.jpg')) {
                $arr[$key]['preview'] = self::$Cache["Config"]['siteurl'] . str_replace(SITE_PATH, "", TEMPLATE_PATH) . $arr[$key]['name'] . '/preview.jpg';
            } else {
                $arr[$key]['preview'] = self::$Cache["Config"]['siteurl'] . 'statics/images/nopic.jpg';
            }

            $arr[$key]['use'] = 0;
            if (self::$Cache["Config"]['theme'] == $arr[$key]['name']) {
                $arr[$key]['use'] = 1;
            }
            $count ++;
        }

        $this->assign('themes', $arr);
        $this->assign('count', $count);
        return $this->fetch();
    }

    /**
     * 风格选择
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function chose()
    {
        $theme = input('theme');
        if (empty($theme)) {
            $this->error("主题名称不能为空！");
        }
        if ($theme == self::$Cache['Config']['theme']) {
            $this->error("主题未改变！", url("Theme/index"));
        }
        $status = Db('config')->where(array('varname' => 'theme'))->update(array('value' => $theme));
        cache('Config',null);
        if ($status !== false) {
            $this->success("更新成功！");
        } else {
            $this->error("更新失败！");
        }
    }
}
